//
//  NewFeatureViewController.swift
//  weibo
//
//  Created by yellow on 15/10/18.
//  Copyright © 2015年 yellow. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "NewFeatureViewCell"

class NewFeatureViewController: UICollectionViewController {
    
    init(){
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.scrollDirection = .Horizontal
        layout.minimumLineSpacing=0
        
        //因为创建collectionView必须要layout才能创建，所以在父类初始化前设置layout
        super.init(collectionViewLayout: layout)
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(NewFeatureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    //隐藏状态栏
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //一共有4张新特性图片
        return 4
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureViewCell
        cell.index = indexPath.item
        return cell
    }
    
    
    //当cell停止拖动后，就会调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算当前cell所在的index
       let ImageIndex = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.size.width)
        if ImageIndex == 3 {
            let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: ImageIndex, inSection: 0))
            
            (cell as! NewFeatureViewCell).showAnimal()
        }
    }

}


//因为NewFeatureViewCell和NewFeatureViewController关系紧密，所以定义为内部类
private class NewFeatureViewCell:UICollectionViewCell{
    
    //根据cell的索引设置对应的图片，并把btn隐藏掉
    var index:Int = 0 {
        didSet{
            backImage?.image = UIImage(named: "new_feature_\(index+1)")
            startBtn?.hidden = true
        }
    }
    
    @objc private func startBtnClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(jump2MainVCNotification, object: "MainViewController")
    }
    
    func showAnimal(){
        startBtn?.hidden = false
        startBtn?.transform = CGAffineTransformMakeScale(0, 0)
        startBtn?.userInteractionEnabled = false
        /*
        设置动画属性具有弹性，主要参数：
        usingSpringWithDamping 0~1 越小越弹
        initialSpringVelocity 初始速度，(10 模仿重力加速度)
        第一个闭包是改变的动画属性
        最后一个闭包是当动画完成后
        */
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            startBtn?.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                //动画完成后启用用户交互
                startBtn?.userInteractionEnabled = true
        }
    }
    
    
    
    //懒加载 view中的控件
    lazy var backImage:UIImageView? = UIImageView()
    lazy var startBtn:UIButton? = UIButton(title: "立即体验",color:UIColor.whiteColor(),imageName: "new_feature_finish_button", actionName: "startBtnClick", target: self)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI(){
        addSubview(backImage!)
        addSubview(startBtn!)
        //cell中的bounds是flowlayout中的itemSize的大小
        backImage?.frame = bounds
        
        //自动布局,使用snapkit进行布局
        startBtn?.snp_makeConstraints(closure: { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.8)
        })
    }
    
    
}
